package com.raychi.demo.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.raychi.demo.dao.MarkDao;
import com.raychi.demo.entity.Mark;
import com.raychi.demo.util.JdbcUtil;
import com.raychi.demo.util.SessionUtil;

public class MarkService {
	
	private MarkDao markDao;
	
	/**
	 * 列出mark的信息 包含学生的姓名
	 * @return mark信息的列表
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public List<Mark> getMark() throws ClassNotFoundException, SQLException{
		Connection conn = null;
		List<Mark> list = null;
		try{
			conn = JdbcUtil.getConnection();
			list = markDao.getMarks(conn, SessionUtil.getStudent().getId());
			conn.commit();
		}catch(Exception e){
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}
		return list;
	}
	
	/**
	 * 列出mark的所有信息
	 * @return mark所有信息的列表
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public List<Mark> listMarks() throws ClassNotFoundException, SQLException{
		Connection conn = null;
		List<Mark> list = null;
		try{
			conn = JdbcUtil.getConnection();
			list = markDao.listMarks(conn);
			conn.commit();
		}catch(Exception e){
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}
		return list;
	}
	
	/**
	 * 删除mark的所有信息
	 * @param mark
	 */
	public void delete(Mark mark){
		Connection conn = null;
		try{
			conn = JdbcUtil.getConnection();
			markDao.delete(mark, conn);
			conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}
	}
	
	/**
	 * 更新mark的信息
	 * @param mark
	 */
	
	public void update(Mark mark){
		Connection conn = null;
		try{
			conn = JdbcUtil.getConnection();
			markDao.update(mark, conn);
			conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}	
	}
	
	/**
	 * 保存mark的信息
	 * @param mark
	 */
	public void save(Mark mark){
		Connection conn = null;
		try{
			conn = JdbcUtil.getConnection();
			markDao.save(mark, conn);
			conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}
	}
	public void setMarkDao(MarkDao markDao) {
		this.markDao = markDao;
	}
	
	public Mark getMarkById(int id) throws ClassNotFoundException, SQLException{
		Connection conn = null;
		Mark mark = null;
		try{
			conn = JdbcUtil.getConnection();
			mark = markDao.getMarkById(conn, id);
			conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}
		return mark;
	}
	
}
